<?php

$mcrp = './../';
include_once($mcrp . 'functions/session.php');
include_once($mcrp . 'functions/contests.php');

if($mcuser->isadmin == 0) 
{
	header('Location: ../');
	exit;
}

if(!isset($_GET['id']))
{
	header('Location: contests.php');
	exit;
}

$contest = GetContestById($_GET['id']);
if($contest === false)
{
	?>
	<html><head><title>µContest - Erreur</title>
	<meta http-equiv="refresh" content="2; URL=contests.php">
	</head>
	<body>
	Epreuve inexistante. Redirection...
	</body></html>
	<?php
	exit(0);
}

if(isset($_GET['do']))
{
	if($_GET['do'] == 'src')
	{
		echo file_get_contents('./../contests/' . $contest->id . '/' . $_GET['file']);
		exit;
	}
	else if($_GET['do'] == 'save')
	{
		if(!isset($_POST['file'])) { echo 'ERREUR : fichier non spécifié'; exit(0); }
		if(!isset($_POST['src'])) { echo 'ERREUR : contenu du fichier non envoyé'; exit(0); }
		$url = $mcrp . 'contests/' . $_GET['id'] . '/' . $_POST['file'];
		$content = str_replace('\r\n', chr(13).chr(10), $_POST['src']);
		$content = str_replace('\n', chr(13).chr(10), $content);
		if(file_put_contents($url, stripslashes($content)) === false)
			echo 'ERREUR : impossible d\'écrire dans le fichier ' . $url;
		else
			echo 'OK : ' . $_POST['file'] . ' mis à jour';
		exit;
	}
	else if($_GET['do'] == 'exec_contest')
	{
		unset($_SESSION['var']);
		chdir($mcrp . 'contests/' . $_GET['id'] . '/');
		include('contest.php');
		exit;
	}
	else if($_GET['do'] == 'test')
	{
		$_SESSION['test'] = 1;
		$dir = $mcrp . 'contests/' . $_GET['id'] . '/';
		chdir($dir);
		include('validation.php');
		unset($_SESSION['test']);
		exit;
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<title>µContest - Test - <?php echo stripslashes($contest->title); ?></title>
	<script type="text/javascript" src="test.js"></script>
	<style type="text/css">
	html, body
	{
		margin: 0;
		padding: 0;
		height: 100%;
	}
	.over
	{
		overflow: auto;
	}
	a
	{
		text-decoration: none;
		color: #A71212;
	}

	a:hover
	{
		color: #501212;
	}
	button, input[type=submit]
	{
		border: 1px solid #501212;
		background-color: white;
	}

	button:hover, input[type=submit]:hover
	{
		border: 1px solid #A71212;
	}

	button:active, input[type=submit]:active
	{
		background-color: #8B90AC;
	}
	</style>
</head>
<body onload="InitTest(<?php echo $contest->id; ?>);">
	<div id="src" class="over" style="height:100%;width:50%;float:left;background-color:#EEEEEE;">
		<div>
			<select id="file" onchange="ChangeFile();">
				<option>contest.php</option>
				<option>validation.php</option>
			</select>
			<span style="height:10px;float:right;padding-right:10px;font-size:1.05em;"><a href="<?php echo MC_ROOT . 'admin/contestedit.php?id=' . $contest->id; ?>"><?php echo stripslashes($contest->title) . ' (' . $contest->id . ')'; ?></a></span>
		</div>
		<center><textarea id="src_area" style="padding:2px;width:98%;" onchange="SrcAreaChanged();" spellcheck="false"></textarea></center>
		<button onclick="SaveSrc();">Enregistrer</button>
		<button onclick="UpdateContest();">Actualiser</button>
		<button onclick="SaveAndUpdate();">Enregistrer et actualiser</button>
		<button onclick="Reload();">Recharger</button>
		<br /><span id="request"></span><span id="result"></span>
	</div>
	<div id="out" style="width:50%;height:100%;float:right;">
		<div id="contest" class="over" style="height:50%;width:100%;background-color:#EEFFEE;"></div>
		<div class="over" style="height:30%;width:100%;background-color:#EEEEFF;">
			<div id="vars" style="width:100%;"></div>
			<button onclick="AddVar();">Ajouter</button>
			<button onclick="RequestTest();">Tester</button>
		</div>
		<div id="validation" class="over" style="height:20%;width:100%;background-color:#FFEEEE;"></div>
	</div>
</body>
</html>